from utils.signal_emitter import SignalEmitter


def on_action_order_by_triggered(main_window, header_text, action):
    order_by_condition = f"ORDER BY \"{header_text}\" {action}"
    main_window.csv_reader_ui.oRDERBYLineEdit.setText(f"\"{header_text}\" {action}")

    original_sql = main_window.csv_reader_ui.sQLLineEdit.text()
    signal_emitter = SignalEmitter()
    signal_emitter.result_signal.connect(main_window.close_load_window)
    # 先判断并删除原始SQL语句中已有的ORDER BY子句
    if "ORDER BY" in original_sql or "order by" in original_sql:
        start_index = original_sql.find("ORDER BY")
        end_index = original_sql.find("LIMIT")
        if end_index == -1:
            original_sql = original_sql[:start_index]
        else:
            original_sql = original_sql[:start_index] + original_sql[end_index:]

    # 如果有新的ORDER BY条件要添加，则进行添加操作
    if order_by_condition:
        original_sql = original_sql[
                       :original_sql.find("LIMIT")] + "" + order_by_condition + " " + original_sql[
                                                                                      original_sql.find(
                                                                                          "LIMIT"):]
    main_window.csv_reader_ui.sQLLineEdit.setText(original_sql)
    main_window.start_query_file_thread()


def on_action_choose_column_triggered(main_window, header_text):
    original_sql = main_window.csv_reader_ui.sQLLineEdit.text()

    if original_sql[:original_sql.find("FROM")].find("*") != -1:
        original_sql = original_sql.replace("SELECT *", "SELECT")
        column_field = f"\"{header_text}\""
    else:
        column_field = f", \"{header_text}\""
    new_sql = original_sql[:original_sql.find("FROM")] + "" + column_field + " " + original_sql[
                                                                                   original_sql.find("FROM"):]

    original_select_text = main_window.csv_reader_ui.sELECTLineEdit.text()
    main_window.csv_reader_ui.sELECTLineEdit.setText(original_select_text + column_field)
    main_window.csv_reader_ui.sQLLineEdit.setText(new_sql)
    pass


def on_select_text_changed(select_text, main_window):
    original_sql = main_window.csv_reader_ui.sQLLineEdit.text()
    select_text = select_text if select_text != '' else '*'
    new_sql = "SELECT " + select_text + " " + original_sql[original_sql.upper().find("FROM"):]
    main_window.csv_reader_ui.sQLLineEdit.setText(new_sql)


def on_where_text_changed(where_text, main_window):
    original_sql = main_window.csv_reader_ui.sQLLineEdit.text()
    if original_sql.upper().find("WHERE") != -1:
        if original_sql.upper().find("ORDER BY") != -1:
            new_sql = (original_sql[:original_sql.upper().find("WHERE") + 6] + "" +
                       where_text + " " + original_sql[original_sql.upper().find("ORDER BY"):])
        else:
            new_sql = (original_sql[:original_sql.upper().find("WHERE") + 6] + "" +
                       where_text + " " + original_sql[original_sql.upper().find("LIMIT"):])
    else:
        if original_sql.upper().find("ORDER BY") != -1:
            new_sql = original_sql[:original_sql.upper().find("ORDER BY")] + "WHERE " + where_text + " " + original_sql[
                                                                                                           original_sql.upper().find(
                                                                                                               "LIMIT"):]
        elif original_sql.upper().find("LIMIT") != -1:
            new_sql = original_sql[:original_sql.upper().find("LIMIT")] + "WHERE " + where_text + " " + original_sql[
                                                                                                        original_sql.upper().find(
                                                                                                            "LIMIT"):]

    if where_text == '':
        new_sql = new_sql.replace("WHERE ", "")
    main_window.csv_reader_ui.sQLLineEdit.setText(new_sql)


def on_order_by_text_changed(order_by_text, main_window):
    original_sql = main_window.csv_reader_ui.sQLLineEdit.text()
    if original_sql.upper().find("ORDER BY") != -1:
        new_sql = (original_sql[:original_sql.upper().find("ORDER BY") + 9] + "" +
                   order_by_text + " " + original_sql[original_sql.upper().find("LIMIT"):])
    else:
        new_sql = original_sql[
                  :original_sql.upper().find("LIMIT")] + "ORDER BY " + order_by_text + " " + original_sql[
                                                                                             original_sql.upper().find(
                                                                                                 "LIMIT"):]

    if order_by_text == '':
        new_sql = new_sql.replace("ORDER BY ", "")
    main_window.csv_reader_ui.sQLLineEdit.setText(new_sql)


def on_limit_text_changed(limit_text, main_window, ):
    original_sql = main_window.csv_reader_ui.sQLLineEdit.text()
    if original_sql.upper().find("LIMIT") != -1:
        new_sql = original_sql[:original_sql.upper().find("LIMIT") + 6] + "" + limit_text
    else:
        new_sql = original_sql[:original_sql.upper().find("LIMIT")] + "LIMIT " + limit_text
    if limit_text == '':
        new_sql = original_sql[:original_sql.upper().find("LIMIT") + 6] + " 1000"

    main_window.csv_reader_ui.sQLLineEdit.setText(new_sql)